<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入出库标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择出库类型" clearable>
          <el-option
            v-for="dict in bus_out_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
     
      <el-form-item label="出库人" prop="outBy">
        <el-input
          v-model="queryParams.outBy"
          placeholder="请输入出库人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in bus_out_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['crm:outStore:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:outStore:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:outStore:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['crm:outStore:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="outStoreList" @selection-change="handleSelectionChange" style="height: calc(100vh - 320px);">
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="ID" align="center" prop="id" fixed="left" width="180"/>
      <el-table-column label="出库标题" align="center" prop="title" width="180" fixed="left">
		  <template #default="scope">
			  <el-link @click="handleOutTitle(scope.row)" type="primary">{{scope.row.title}}</el-link>
		  </template>
	  </el-table-column>
      <el-table-column label="出库类型" align="center" prop="type" width="120" fixed="left">
        <template #default="scope">
          <dict-tag :options="bus_out_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="出库时间" align="center" prop="outTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.outTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库人" align="center" prop="outBy" />
      <el-table-column label="出库原因" align="center" prop="reason" show-overflow-tooltip width="180"/>
	  <el-table-column label="总进货金额" align="center" prop="sumPurchase" />
      <el-table-column label="总销售金额" align="center" prop="sumSale" />
      <el-table-column label="总数" align="center" prop="total" />
      <el-table-column label="出库门店" align="center" prop="belDeptId" />
      <!-- <el-table-column label="审批人" align="center" prop="auditorBy" />
      <el-table-column label="审批时间" align="center" prop="auditorTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="拒绝原因" align="center" prop="rejectReason" /> -->
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
	  <el-table-column label="状态" align="center" prop="status" fixed="right" width="120">
	    <template #default="scope">
	      <dict-tag :options="bus_out_status" :value="scope.row.status"/>
	    </template>
	  </el-table-column>
     <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['crm:outStore:edit']">审批</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['crm:outStore:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品出库对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body :close-on-click-modal="false" >
      <el-form ref="outStoreRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出库标题" prop="title">
          <el-input v-model="form.title" :disabled="form.status=='3'||form.status=='1'" placeholder="请输入出库标题" />
        </el-form-item>
        <el-form-item label="出库类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择出库类型" :disabled="form.status=='3'||form.status=='1'">
            <el-option
              v-for="dict in bus_out_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出库时间" prop="outTime" >
          <el-date-picker clearable
		  :disabled="form.status=='3'||form.status=='1'"
            v-model="form.outTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择出库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出库原因" prop="reason" >
          <el-input v-model="form.reason" type="textarea" maxlength="200" :disabled="form.status=='3'||form.status=='1'" placeholder="请输入出库原因" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :disabled="form.status=='3'||form.status=='1'" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" :disabled="form.status=='3'||form.status=='1'">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
	
	<outGoods ref="outGoodsRef" @closed="outGoodsClosed"/>
	
  </div>
</template>

<script setup name="OutStore">
import { listOutStore, getOutStore, delOutStore, addOutStore, updateOutStore } from "@/api/crm/outStore";
import outGoods from '../outGoods/index.vue'

const { proxy } = getCurrentInstance();
const { bus_out_type, bus_out_status } = proxy.useDict('bus_out_type', 'bus_out_status');

const outStoreList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const outGoodsRef = ref();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
	orderByColumn:'out_time',
	isAsc:'desc',
    title: null,
    type: null,
    outTime: null,
    outBy: null,
    reason: null,
    sum: null,
    total: null,
    belDeptId: null,
    auditorBy: null,
    auditorTime: null,
    status: null,
    rejectReason: null,
  },
  rules: {
	  title: [
	      { required: true, message: '请输入出库标题', trigger: 'blur' }
	  ],
	  type: [
	      { required: true, message: '请选择出库类型', trigger: 'change' }
	  ],
	  outTime: [
	      { required: true, message: '请选择出库时间', trigger: 'change' }
	  ],
	  reason: [
	      { required: true, message: '请输入出库原因', trigger: 'blur' }
	  ]
	  
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商品出库列表 */
function getList() {
  loading.value = true;
  listOutStore(queryParams.value).then(response => {
    outStoreList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    type: null,
    outTime: null,
    outBy: null,
    reason: null,
    sum: null,
    total: null,
    belDeptId: null,
    auditorBy: null,
    auditorTime: null,
    status: null,
    rejectReason: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("outStoreRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商品出库";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getOutStore(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商品出库";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["outStoreRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOutStore(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOutStore(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除商品出库编号为"' + _ids + '"的数据项？').then(function() {
    return delOutStore(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('crm/outStore/export', {
    ...queryParams.value
  }, `outStore_${new Date().getTime()}.xlsx`)
}


function handleOutTitle(row){
	outGoodsRef.value.openWin(row.id)
}

function outGoodsClosed(){
	getList();
}

getList();
</script>
